﻿using System.Xml.Linq;
using PTop.Domain.ViewModel.Wechat;

namespace PTop.Front.Areas.Wechat.Models
{
    public class VoiceMessage : WechatMsgBase
    {
        /// <summary>
        /// 接收消息模版
        /// </summary>
        private static string _template;

        public long MsgId { get; set; }
        public string MediaId { get; set; }


        public override string MsgType
        {
            get { return "voice"; }
        }

        public override string Template
        {
            get
            {
                if (string.IsNullOrEmpty(_template))
                {
                    _template = @"<xml>
                                <ToUserName><![CDATA[{0}]]></ToUserName>
                                <FromUserName><![CDATA[{1}]]></FromUserName>
                                <CreateTime>{2}</CreateTime>
                                <MsgType><![CDATA[voice]]></MsgType>
                                <MediaId><![CDATA[{3}]]></MediaId>
                                <Format><![CDATA[amr]]></Format>
                                <MsgId>{4}</MsgId>
                            </xml>";
                }

                return _template;
            }
        }
        /// <summary>
        /// 从xml数据加载文本消息
        /// </summary>
        /// <param name="xml"></param>
        public static VoiceMessage LoadFromXml(string xml)
        {
            VoiceMessage tm = null;
            if (!string.IsNullOrEmpty(xml))
            {
                XElement element = XElement.Parse(xml);
                if (element != null)
                {
                    tm = new VoiceMessage();
                    tm.FromUserName = element.Element(WeChatCommon.FROM_USERNAME).Value;
                    tm.ToUserName = element.Element(WeChatCommon.TO_USERNAME).Value;
                    tm.CreateTime = element.Element(WeChatCommon.CREATE_TIME).Value;
                    tm.MsgId = long.Parse(element.Element(WeChatCommon.MSG_ID).Value);
                    tm.MediaId = element.Element("MediaId").Value;
                }
            }
            return tm;
        }
        public override string HandleRequest()
        {
            //LogServiceHelper.Log("上海", true, 3, "voiceTest", "1-", "", "");
            var replymsg = "/微笑您发送的信息我们已收到，我们会尽快给您答复，感谢您对搜房网的支持。紧急问题请拨打搜房帮全国咨询热线：400-630-6888";
            //if (DateTime.Now <= Convert.ToDateTime(ConfigHelper.GetConfig("WeChatAutoReplyDate", "2016-06-08 12:00:00")))
            //{
            //    replymsg += "\n6月8日上午10点\n互动直播《如何高效使用手机搜房帮》，<a href='http://h.eqxiu.com/s/6df4Z1b4?eqrcode=1'>点此进入</a>";
            //}
            //普通消息
            var msg = new TextMessage()
            {
                Content = replymsg,
                CreateTime = WeChatCommon.GetNowTime(),
                FromUserName = this.ToUserName,
                ToUserName = this.FromUserName,
            };
            msg.GenerateContent();
            //转给多客服
            var cmsg = new CustomerMessage()
            {
                CreateTime = WeChatCommon.GetNowTime(),
                FromUserName = this.ToUserName,
                ToUserName = this.FromUserName,
            };
            return cmsg.GenerateContent();
        }
    }
}